package com.example.administrator.managesystem.network;
import android.Manifest;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.example.administrator.managesystem.base.MyApplication;
import com.example.administrator.managesystem.utils.AndroidUtils;
import com.example.administrator.managesystem.utils.GlobalConfig;
import com.example.administrator.managesystem.utils.Sha1;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.example.administrator.managesystem.utils.GlobalConfig.APPID;
import static com.example.administrator.managesystem.utils.GlobalConfig.APPSECRET;



public abstract class RetrofitHelper {
    public static final int TIME_OUT = 10_000;
    public static final int TIME_OUT_LONG = 5_000_000;
    private static final String TAG = "RetrofitHelper";
    private Retrofit retrofit;
    protected Context mContext;

    private static final int DEFAULT_TIMEOUT = 15000;

    protected RetrofitHelper(Context mContext) {
        this.mContext = mContext;
        retrofit = getRetrofitBuild(TIME_OUT);
    }

    @NonNull
    private Retrofit getRetrofitBuild(int timeOutMillSeconds) {
        Gson gson = new GsonBuilder().setLenient().create();
        return new Retrofit.Builder()
                .baseUrl(GlobalConfig.ONLINE_HTTP)
                .client(getClient())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())//新的配置
                .build();
    }

    public <T>T getService(Class<T> c) {
        if (c != null && retrofit != null) {
            return retrofit.create(c);
        }
        return null;
    }


    public OkHttpClient getClient() {
        //缓存
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        if (true){
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        }else {
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE);
        }
        File cacheFile = new File(MyApplication.getInstances().getCacheDir(), "cache");
        Cache cache = new Cache(cacheFile, 1024 * 1024 * 100); //100Mb
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                // 向Request Header添加一些业务相关数据，如APP版本，token
                .readTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
                .addInterceptor(loggingInterceptor)
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request build = chain.request()
                                .newBuilder()
                                .addHeader("Content-Type", "application/json")
                                .build();
                        return chain.proceed(build);
                    }
                })
                .cache(cache)
                .retryOnConnectionFailure(true);
        return builder.build();
    }

}


